﻿using System;
using System.Web;

namespace Medora.Core
{
    public class CacheHelper : ICacheHelper
    {

    public void Add<T>(T o, string key)
    {
        // NOTE: Apply expiration parameters as you see fit.
        // I typically pull from configuration file.

        // In this example, I want an absolute
        // timeout so changes will always be reflected
        // at that time. Hence, the NoSlidingExpiration.
        HttpContext.Current.Cache.Insert(
            key,
            o,
            null,
            DateTime.Now.AddMinutes(1440),
            System.Web.Caching.Cache.NoSlidingExpiration);
    }

   
    public void Clear(string key)
    {
        HttpContext.Current.Cache.Remove(key);
    }

  
    public bool Exists(string key)
    {
        return HttpContext.Current.Cache[key] != null;
    }

   
    public bool Get<T>(string key, out T value)
    {
        try
        {
            if (!Exists(key))
            {
                value = default(T);
                return false;
            }

            value = (T) HttpContext.Current.Cache[key];
        }
        catch
        {
            value = default(T);
            return false;
        }

        return true;
    }
}
    

   
}